更新 您所在的位置:网站首页 gorm update updates 更新

更新

2023-10-08 11:44| 来源: 网络整理| 查看: 265

更新所有字段

Save会更新所有字段,即使你没有赋值

db.First(&user)user.Name = "jinzhu 2"user.Age = 100db.Save(&user)//// UPDATE users SET name='jinzhu 2', age=100, birthday='2016-01-01', updated_at = '2013-11-17 21:34:10' WHERE id=111; 更新修改字段

如果你只希望更新指定字段,可以使用Update或者Updates

// 更新单个属性,如果它有变化db.Model(&user).Update("name", "hello")//// UPDATE users SET name='hello', updated_at='2013-11-17 21:34:10' WHERE id=111;// 根据给定的条件更新单个属性db.Model(&user).Where("active = ?", true).Update("name", "hello")//// UPDATE users SET name='hello', updated_at='2013-11-17 21:34:10' WHERE id=111 AND active=true;// 使用 map 更新多个属性,只会更新其中有变化的属性db.Model(&user).Updates(map[string]interface{}{"name": "hello", "age": 18, "actived": false})//// UPDATE users SET name='hello', age=18, actived=false, updated_at='2013-11-17 21:34:10' WHERE id=111;// 使用 struct 更新多个属性,只会更新其中有变化且为非零值的字段db.Model(&user).Updates(User{Name: "hello", Age: 18})//// UPDATE users SET name='hello', age=18, updated_at = '2013-11-17 21:34:10' WHERE id = 111;// 警告:当使用 struct 更新时,GORM只会更新那些非零值的字段// 对于下面的操作,不会发生任何更新,"", 0, false 都是其类型的零值db.Model(&user).Updates(User{Name: "", Age: 0, Actived: false}) 更新选定字段

如果你想更新或忽略某些字段,你可以使用 Select,Omit

db.Model(&user).Select("name").Updates(map[string]interface{}{"name": "hello", "age": 18, "actived": false})//// UPDATE users SET name='hello', updated_at='2013-11-17 21:34:10' WHERE id=111;db.Model(&user).Omit("name").Updates(map[string]interface{}{"name": "hello", "age": 18, "actived": false})//// UPDATE users SET age=18, actived=false, updated_at='2013-11-17 21:34:10' WHERE id=111; 无 Hooks 更新

上面的更新操作会自动运行 model 的 BeforeUpdate, AfterUpdate 方法,更新 UpdatedAt 时间戳, 在更新时保存其 Associations, 如果你不想调用这些方法,你可以使用 UpdateColumn, UpdateColumns

// 更新单个属性,类似于 `Update`db.Model(&user).UpdateColumn("name", "hello")//// UPDATE users SET name='hello' WHERE id = 111;// 更新多个属性,类似于 `Updates`db.Model(&user).UpdateColumns(User{Name: "hello", Age: 18})//// UPDATE users SET name='hello', age=18 WHERE id = 111; 批量更新

批量更新时 Hooks 不会运行

db.Table("users").Where("id IN (?)", []int{10, 11}).Updates(map[string]interface{}{"name": "hello", "age": 18})//// UPDATE users SET name='hello', age=18 WHERE id IN (10, 11);// 使用 struct 更新时,只会更新非零值字段,若想更新所有字段,请使用map[string]interface{}db.Model(User{}).Updates(User{Name: "hello", Age: 18})//// UPDATE users SET name='hello', age=18;// 使用 `RowsAffected` 获取更新记录总数db.Model(User{}).Updates(User{Name: "hello", Age: 18}).RowsAffected 使用 SQL 表达式更新DB.Model(&product).Update("price", gorm.Expr("price * ? + ?", 2, 100))//// UPDATE "products" SET "price" = price * '2' + '100', "updated_at" = '2013-11-17 21:34:10' WHERE "id" = '2';DB.Model(&product).Updates(map[string]interface{}{"price": gorm.Expr("price * ? + ?", 2, 100)})//// UPDATE "products" SET "price" = price * '2' + '100', "updated_at" = '2013-11-17 21:34:10' WHERE "id" = '2';DB.Model(&product).UpdateColumn("quantity", gorm.Expr("quantity - ?", 1))//// UPDATE "products" SET "quantity" = quantity - 1 WHERE "id" = '2';DB.Model(&product).Where("quantity > 1").UpdateColumn("quantity", gorm.Expr("quantity - ?", 1))//// UPDATE "products" SET "quantity" = quantity - 1 WHERE "id" = '2' AND quantity > 1; 修改 Hooks 中的值

如果你想修改 BeforeUpdate, BeforeSave 等 Hooks 中更新的值,你可以使用 scope.SetColumn, 例如:

func (user *User) BeforeSave(scope *gorm.Scope) (err error) { if pw, err := bcrypt.GenerateFromPassword(user.Password, 0); err == nil { scope.SetColumn("EncryptedPassword", pw) }} 其它更新选项// 为 update SQL 添加其它的 SQLdb.Model(&user).Set("gorm:update_option", "OPTION (OPTIMIZE FOR UNKNOWN)").Update("name", "hello")//// UPDATE users SET name='hello', updated_at = '2013-11-17 21:34:10' WHERE id=111 OPTION (OPTIMIZE FOR UNKNOWN);


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有